package com.zhou.service.impl;

import com.zhou.pojo.Register;
import com.zhou.service.ClientService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ClientServiceImpl implements ClientService, InitializingBean {
    @Value("${server.port}")
    private String port; //端口号

    @Value("${spring.application.name}")
    private String name; //服务名称

    private RestTemplate restTemplate = new RestTemplate();

    @Override
    public void pull() throws Exception {
        String ip = InetAddress.getLocalHost().getHostAddress();//获取ip地址

        //封装信息
        Register register = new Register();
        register.setPort(port); //封装端口号
        register.setIp(ip); //封装ip地址
        register.setName(name); //封装姓名
        String statusTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        register.setStatus(statusTime); //封装状态

        System.out.println(port + ":" + ip + name);
        String url = "http://localhost:10000/server/add";
        restTemplate.postForObject(url, register, Register.class);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        this.pull();
    }
}
